<?php

/**
 * uniBoard ACP Cache Browser
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_cache{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Cache Files
	 *
	 * @var array
	 */
	
	private $cache_index	= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
			
		// Build Cache Index
		$this -> buildCacheIndex();
			
		// Cache output
		ob_start();
				
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_half',
				$appCore -> lang['acp_cache_list_file']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['column_size']
			)
		));
		
		// Draw Files?
		if ( isset( $this -> cache_index[0]))
		{
			// Reiterate index
			foreach ( $this -> cache_index as $file)
			{
				// Draw file
				echo $appCore -> output -> tableRow('
				<div class="cell left_float width_half">
					<b>' . $file[0] . '</b>' . ( isset( $file[1][0]) ? '<div class="item_description">' . $file[1] . '</div>' : '') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					' . $file[2] . '
				</div>');
			}
		}
		else
		{
			// Draw message instead
			echo $appCore -> output -> tableRow( '<div class="cell">' . $appCore -> lang['acp_cache_list_empty'] . '</div>');	
		}
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_cache'], array( $appCore -> adminLink( 'act=cache') => $appCore -> lang['acp_cache']), ob_get_clean());
				
	}

	// ------------------------------------------------------------------------

	/**
	 * Builds Cache Files Index
	 *
	 */
	
	function buildCacheIndex(){
		
		// PHP extension lenght
		$ext_lenght = strlen( PHP_EXT);
		
		// Langauages
		$languages = $this -> appCore -> getLanguages();
		
		// Use Glob?
		if ( USE_GLOB)
		{		
			// GLOB Makes whole thing much easier
			foreach ( $languages as $lang_id => $lang_name)
			{
				// Lang Exists?
				if ( file_exists( CACHE_PATH . 'lang_' . $lang_id . '/index.html'))
				{
					// Map directory
					$lang_map = glob( CACHE_PATH . 'lang_' . $lang_id . '/*' . PHP_EXT);
					
					// Map lenght
					$map_size = count( $lang_map);
					
					// Lang Dir Lenght
					$dir_lenght = strlen( CACHE_PATH . 'lang_' . $lang_id . '/');
					
					// Reiterate map
					for ( $i = 0; $i < $map_size; $i ++)
					{
						// File name
						$file = substr( $lang_map[$i], $dir_lenght, strlen( $lang_map[$i]) - $dir_lenght - $ext_lenght);
					
						// Put in teh index
						$this -> cache_index[] = array(
							0 => 'lang_' . $lang_id . '/' . $file,
							1 => sprintf( $this -> appCore -> lang['acp_cache_lang_string'], $lang_name) . ': ' . $this -> langFileRole( $file),
							2 => class_strings::countSize( filesize( $lang_map[$i]))
						);
					}
					
					// Kill raw map
					unset( $lang_map);
				}
			}
			
			// Now regular lang files
			$cache_files = glob( CACHE_PATH . '*' . PHP_EXT);
			
			// Map lenght
			$map_size = count( $cache_files);
			
			// Lang Dir Lenght
			$dir_lenght = strlen( CACHE_PATH);
			
			// Reiterate map
			for ( $i = 0; $i < $map_size; $i ++)
			{
				// File name
				$file = substr( $cache_files[$i], $dir_lenght, strlen( $cache_files[$i]) - $dir_lenght - $ext_lenght);
			
				// Put in teh index
				$this -> cache_index[] = array(
					0 => $file,
					1 => $this -> cacheFileRole( $file),
					2 => class_strings::countSize( filesize( $cache_files[$i]))
				);
			}
			
		}
		else
		{
			// Go painfull way, check each language
			
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns Lang File description
	 *
	 * @param string $lang_file
	 * @return string
	 */
	
	function langFileRole( $lang_file){
		
		return ( isset( $this -> appCore -> lang['acp_cache_lang_' . $lang_file]) ? $this -> appCore -> lang['acp_cache_lang_' . $lang_file] : $this -> appCore -> lang['acp_cache_lang_unknown']);
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns Cache File description
	 *
	 * @param string $cache_file
	 * @return string
	 */
	
	function cacheFileRole( $cache_file){
		
		return ( isset( $this -> appCore -> lang['acp_cache_file_' . $cache_file]) ? $this -> appCore -> lang['acp_cache_file_' . $cache_file] : $this -> appCore -> lang['acp_cache_file_unknown']);
		
	}
	
}